package com.cdks.wuxiu.api.controller;


import com.cdks.wuxiu.api.component.aliyun.CloudStorage;
import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.component.huawei.Moderation;
import com.cdks.wuxiu.api.model.param.ChatAddIdiomParam;
import com.cdks.wuxiu.api.model.param.ChatDelIdiomParam;
import com.cdks.wuxiu.api.model.param.ChatEditIdiomParam;
import com.cdks.wuxiu.api.model.param.ChatGetIdiomParam;
import com.cdks.wuxiu.api.model.param.ChatGetRecordParam;
import com.cdks.wuxiu.api.model.param.ChatSendIdiomParam;
import com.cdks.wuxiu.api.model.param.ChatSendMsgParam;
import com.cdks.wuxiu.api.service.BookService;
import com.cdks.wuxiu.api.service.CallService;
import com.cdks.wuxiu.api.service.ChatService;
import com.cdks.wuxiu.api.service.MessageService;
import com.cdks.wuxiu.api.service.UserService;
import com.cdks.wuxiu.api.util.HttpUtils;
import com.cdks.wuxiu.api.util.SmsUtils;
import com.cdks.wuxiu.api.util.TimeUtils;
import com.cdks.wuxiu.common.model.api.ApiResult;
import com.cdks.wuxiu.entity.message.ChatPrivateData;
import com.cdks.wuxiu.entity.mysql.BookConsultInfo;
import com.cdks.wuxiu.entity.mysql.CallSheetInfo;
import com.cdks.wuxiu.entity.mysql.UserBaseInfo;
import com.cdks.wuxiu.entity.mysql.UserIdiomInfo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;


@Slf4j
@Api(tags = "在线聊天")
@RestController
@RequestMapping("/chat")
public class ChatController {
    @Resource
    private MessageService messageService;
    @Resource
    private UserService userService;
    @Resource
    private CallService callService;    
    @Resource
    private ChatService chatService;
    @Resource
    private BookService bookService;


    private int checkBook(long senderId, long recverId, long nowTime)
    {
        //查找双方在当前时间是否有在预约时段中并且未结束的服务
        BookConsultInfo order  = bookService.getBusySheetBothUser(senderId, recverId, nowTime);
        if (order == null)
        {
            return 0;
        }

        return bookService.autoSignIn(order, senderId);
    }

    private int checkMessage(int type, long senderId, ChatPrivateData data, StringBuilder checkedText)
    {
        int checkResult  = 0;//内容审核结果。0：通过，-10：质检失败，-11：违规信息，-12：加联系方式

        switch(type)
        {
            case Constant.MSG_TYPE_TEXT:
            {
                checkResult  = Moderation.checkText(data.getContent().trim(), checkedText);

                break;
            }
            case Constant.MSG_TYPE_IMAGE:
            {
                checkResult  = Moderation.checkImage("chat/" + senderId + "/" + data.getFileUri());

                break;
            }
            case Constant.MSG_TYPE_VOICE:
            {
/*
                JSONObject json  = new JSONObject();
                json.set("userId", senderId);
                json.set("fileUri", data.getFileUri());

                //依图语音检测
                String res  = HttpUtils.send_post_request("http://localhost:8183/moderation/audio/checkFile.php", null, JSONUtil.toJsonStr(json));
                JSONObject obj  = JSONUtil.parseObj(res);
                if (obj.getInt("code") != null)
                {
                    checkResult  = obj.getInt("code");
                }
                else
                {
                    checkResult  = -1;
                    
                    log.error("moderation failed: data=" + JSONUtil.toJsonStr(data) + ", res=" + res);
                }
*/
                //华为语音检测
                checkResult  = Moderation.checkAudio("chat/" + senderId + "/" + data.getFileUri());

                break;
            }
        }

        return checkResult;
    }

    @ApiOperation("用户发送聊天消息")
    @PostMapping("/sendMsg")
    public ApiResult<Object> sendMsg(@RequestBody @Valid ChatSendMsgParam param) {
        if ((param.getType() != Constant.MSG_TYPE_IMAGE) && (param.getData().length() > 500))
        {
            return ApiResult.failed(10, "发送失败，字数太多");
        }

        long senderId  = param.getUserId();
        long recverId  = param.getDestId();
        if (senderId == recverId)
        {
            return ApiResult.failed(-10, "接收方信息错误");
        }

        String umid  = TimeUtils.getLocalDateTime("yyyyMMddHHmmssSSS");
        ChatPrivateData data  = BeanUtil.copyProperties(JSONUtil.parseObj(param.getData()), ChatPrivateData.class);
        UserBaseInfo sender  = param.getUserInfo();
        //更新用户空闲时间
        userService.setUserIdleTime(sender);
        //保存云存文件
        if (((param.getBranch() == 300) && (param.getSerial() > 2)) || ((param.getBranch() == 310) && (param.getSerial() > 8)))
        {
            if (data.getFileUri() != null)
            {
                if (sender.getType() == Constant.ACCOUNT_TYPE_KEFU)
                {
                    CloudStorage.saveChatFile(1001, data.getFileUri());
                }
                else
                {
                    CloudStorage.saveChatFile(senderId, data.getFileUri());
                }
            }
        }

        //发送方是客服
        if (sender.getType() == Constant.ACCOUNT_TYPE_KEFU)
        {
            JSONObject json  = new JSONObject();
            json.set("userId", recverId);
            json.set("kefuId", senderId);
            json.set("type", param.getType());
            json.set("data", param.getData());

            String ret  = HttpUtils.send_post_request("http://localhost:8183/kefu/sendKefuMessage.php", null, JSONUtil.toJsonStr(json));
            JSONObject res  = JSONUtil.parseObj(ret);
            if (res == null)
            {
                return ApiResult.failed(10, "发送失败，请稍后重试");
            }
                        
            if (res.getInt("code") == 0)
            {
                json  = new JSONObject();
                json.set("umid", umid);                
                return ApiResult.success(json);
            }
            else
            {
                return ApiResult.failed(10, res.getStr("hint"));
            }
        }
        else
        {
            int ttl  = messageService.ttlChatBanTime(senderId);
            if (ttl > 0)
            {
                int checkResult  = checkMessage(param.getType(), senderId, data, null);
                if (checkResult < -11)
                {
                    ttl  = messageService.addChatBanCount(senderId);
                }

                String expire  = "";
                int second   = ttl % 60;
                int minutes  = (ttl % 3600) / 60;
                int hours    = ttl / 3600;
                if (hours > 0)
                {
                    expire  = hours + "小时";
                }
                else if (minutes > 0)
                {
                    expire  = minutes + "分钟";
                }
                else
                {
                    expire  = second + "秒";
                }

                //给发送方发送提示消息
                ChatPrivateData hint  = new ChatPrivateData();
                hint.setContent("发送失败：因你涉嫌发送违规信息，已被临时禁言" + expire);
                messageService.sendUserMessage(recverId, senderId, Constant.MSG_TYPE_HINT, hint, false);
                //保存消息记录为待审
                messageService.saveAuditMessage(senderId, recverId, param.getType(), param.getData(), "违规禁言：" + expire);
                //发送短信通知
                SmsUtils.sendManageNotice("用户" + senderId + "消息待审");

                return ApiResult.failed(10, "发送失败");
            }
        }

        //检查接收方账号状态
        UserBaseInfo recver  = userService.getUserById(recverId);
        if (recver == null)
        {
            return ApiResult.failed(10, "发送失败，对方账号异常");
        }

        if (recver.getStatus() == Constant.ACCOUNT_STATUS_BANNED)
        {
            return ApiResult.failed(10, "对方违规已被封号");
        }

        if (recver.getStatus() == Constant.ACCOUNT_STATUS_DELETED)
        {
            return ApiResult.failed(10, "对方已注销账号");
        }

        //接收方是客服
        if ((recverId == Constant.USERID_SERVER) || (recver.getType() == Constant.ACCOUNT_TYPE_KEFU))
        {
            JSONObject json  = new JSONObject();
            json.set("userId", senderId);
            json.set("type", param.getType());
            json.set("data", param.getData());

            String ret  = HttpUtils.send_post_request("http://localhost:8183/kefu/sendUserMessage.php", null, JSONUtil.toJsonStr(json));
            JSONObject res  = JSONUtil.parseObj(ret);
            if (res == null)
            {
                return ApiResult.failed(10, "发送失败，请稍后重试");
            }

            if (res.getInt("code") == 0)
            {
                json  = new JSONObject();
                json.set("umid", umid);                
                return ApiResult.success(json);
            }
            else
            {
                return ApiResult.failed(10, res.getStr("hint"));
            }            
        }

        //测试账号只能与测试账号互通（这里只判断发送方是测试账号的情况，接收方是测试账号时由后面的同类账号互通逻辑处理）
        if ((sender.getType() == Constant.ACCOUNT_TYPE_TEST) && (recver.getType() != Constant.ACCOUNT_TYPE_TEST))
        {
            return ApiResult.failed(10, "测试账号只能与测试账号互通");
        }

        boolean isBlock  = userService.isBlocked(recverId, senderId);
        if (isBlock)
        {
            ChatPrivateData hint  = new ChatPrivateData();
            hint.setContent("发送失败，你已被对方拉黑");
            messageService.sendUserMessage(recverId, senderId, Constant.MSG_TYPE_HINT, hint, false);

            return ApiResult.failed(10, "发送失败，你已被对方拉黑");
        }

        long nowTime  = TimeUtils.getTimeSecond();
        //检查是否有未结束的预约咨询服务
        int ret  = checkBook(senderId, recverId, nowTime);
        if (ret < 0)
        {
            return ApiResult.failed(10, "发送失败：您有预约服务待处理");              
        }

        Boolean isReplay  = false;
        Boolean isInCall  = false;
        Boolean isInBook  = (ret == 0) ? false : true;
        //在与当前用户预约的咨询服务期间不做消息频控
        if (!isInBook)
        {
            //消息频控：连续发送消息对方未回复，判断双方是否在通话中，如果不在则不能再发送
            int rate  = messageService.addMessageRate(senderId, recverId);
            if (rate > Constant.MSG_TIMES_LIMIT)
            {
                CallSheetInfo sheet  = callService.getStartedCallBothUser(senderId, recverId);
                if (sheet == null)
                {
                    String expire  = "";
                    int ttl      = messageService.ttlMessageRate(senderId, recverId);
                    int second   = ttl % 60;
                    int minutes  = (ttl % 3600) / 60;
                    int hours    = ttl / 3600;
                    if (hours > 0)
                    {
                        expire  = hours + "小时";
                    }
                    else if (minutes > 0)
                    {
                        expire  = minutes + "分钟";
                    }
                    else
                    {
                        expire  = second + "秒";
                    }

                    //给发送方发送提示消息
                    ChatPrivateData hint  = new ChatPrivateData();
                    hint.setContent("发送失败：对方当前正忙。为避免骚扰，需要对方回复或" + expire + "后您才能继续给对方发送消息。");
                    messageService.sendUserMessage(recverId, senderId, Constant.MSG_TYPE_HINT, hint, false);

                    if ((sender.getAuthRole() == Constant.AUTH_ROLE_CONSULT) || (recver.getAuthRole() == Constant.AUTH_ROLE_CONSULT))
                    {
                    }
                    else
                    {
                        JSONObject json  = new JSONObject();
                        json.set("scheme", "xinsu://call/callout?answer=" + recverId);

                        return ApiResult.failed(10, "为避免骚扰，需要对方回复或" + expire + "后您才能继续给对方发送消息。可以试试语音通话，即可随时畅聊。", json);
                    }

                    return ApiResult.failed(10, "发送失败");
                }
            }
            else
            {
                if ((param.getType() == Constant.MSG_TYPE_TEXT) && ((data.getContent().length() > 20) || (rate == 3)))
                {
                    ChatPrivateData hint  = new ChatPrivateData();
                    hint.setContent("【温馨提示】打字太累？可以试试发送语音消息。");
                    messageService.sendUserMessage(recverId, senderId, Constant.MSG_TYPE_HINT, hint, false);  
                }

                //删除反向消息频控。删除成功则说明是回复消息（如果是连续回复会删除失败，因为已经被删除过了）
                ret  = messageService.delMessageRate(recverId, senderId);
                if (ret == 0)
                {
                    isReplay  = true;
                }
            }

            //获取用户最后一条消息md5
            String md5  = messageService.getLastMessageMd5(senderId);
            //重复消息过滤
            if ((md5 != null) && (md5.equals(param.getMd5())))
            {
                messageService.setLastMessageMd5(senderId, param.getMd5());
                //保存消息记录为待审
                messageService.saveAuditMessage(senderId, recverId, param.getType(), param.getData(), "重复消息");
                
                //发送短信通知
                SmsUtils.sendManageNotice("用户" + senderId + "发送重复消息");
            }
            else
            {
                messageService.setLastMessageMd5(senderId, param.getMd5());
            }

            //检查用户发送消息时是否与其他人在通话中
            CallSheetInfo sheet  = callService.getBusyCallAnyUser(param.getUserId(), param.getUserId());
            if (sheet != null)
            {
                if (sheet.getCallerId().equals(param.getDestId()) || sheet.getAnswerId().equals(param.getDestId()))
                {
                    isInCall  = true;
                }
            }
        }

        //接收方已认证或语音消息需要收费
        //接收方已认证5条消息之后收费
        if (recver.getAuthRole() > 0)
        {
            int cost  = (param.getType() == Constant.MSG_TYPE_VOICE) ? Constant.PRICE_MESSAGE_VOICE : Constant.PRICE_MESSAGE_OTHER;
            int count  = messageService.addMessageCount(senderId, recverId);
            if (count < 5)
            {
                cost  = 0;
            }
/*
            UserFriendInfo info  = userService.queryFriend(recverId, recverId);
            if (info != null)
            {
                cost  = 0;
            }
*/
            //计费
            if (cost > 0)
            {
                ret  = chatService.charge(senderId, recverId, cost);
                if (ret == 0)
                {
                    ChatPrivateData hint  = new ChatPrivateData();
                    hint.setContent("聊天收益" + cost + "金币已到账");
                    messageService.sendUserMessage(senderId, recverId, Constant.MSG_TYPE_HINT, hint, false);
                }
                else
                {
                    //删除云存
                    if (Constant.MSG_TYPE_VOICE == param.getType())
                    {
                        CloudStorage.deleteChatFile(param.getUserId(), data.getFileUri());
                    }

                    //消息频控计数减一
                    messageService.decMessageRate(senderId, recverId);
                    
                    JSONObject json  = new JSONObject();
                    json.set("scheme", "xinsu://bean/recharge");

                    return ApiResult.failed(11, "余额不足。请前往充值", json);
                }
            }
        }

        //内容审核
        StringBuilder checkedText = new StringBuilder();
        int checkResult  = checkMessage(param.getType(), senderId, data, checkedText);
        //质检不通过的消息设为待审，通过的消息设为已审
        if (checkResult < 0)
        {
            //删除消息频控，否则如果是认证用户发送的消息被对方回复后会发奖励
            //这里删除消息频控会导致用户在达到消息频控后发送一条质检不通过的消息就又能继续发送消息，所以暂时取消
            //messageService.delMessageRate(senderId, recverId);
            //发送短信通知
            SmsUtils.sendManageNotice("用户" + senderId + "消息待审");
        }

        ret  = messageService.saveUserMessage(senderId, recverId, param.getType(), param.getData());
        if (ret < 0)
        {
            return ApiResult.failed(10, "发送失败");
        }

        //同类账号才能互通
        if (!sender.getType().equals(recver.getType()))
        {
            JSONObject json  = new JSONObject();
            json.set("umid", umid);            
            return ApiResult.success(json);
        }

        int type  = param.getType();
        //质检未通过统一按文字消息发送
        if ((checkResult < -11) && (sender.getIsWhite() == 0))
        {
            data  = new ChatPrivateData();
            type  = Constant.MSG_TYPE_TEXT;

            if (param.getType() == Constant.MSG_TYPE_TEXT)
            {
                data.setContent(checkedText.toString());
            }
            else
            {
                data.setContent("[内容包含违规信息，已被屏蔽]");
            }

            //记录发送违规消息次数
            messageService.addChatBanCount(senderId);
        }

        messageService.sendUserMessage(senderId, recverId, type, data, umid, (recver.getOnlineStatus() == 0));

        //给用户发送提示消息
        if (checkResult < -11)
        {
            ChatPrivateData hint  = new ChatPrivateData();
            if (checkResult == -12)
            {
                messageService.saveAuditMessage(senderId, recverId, param.getType(), param.getData(), "离线交易");

                // 给接收发提示消息
                if (sender.getIsWhite() == 0)
                {
                    hint.setContent("温馨提示：为了您的安全，本平台禁止添加第三方联系方式。请勿泄露手机、微信、QQ、住址等私密信息，注意保护个人隐私，谨防上当受骗。");
                    messageService.sendUserMessage(senderId, recverId, Constant.MSG_TYPE_HINT, hint, false);
                }
            }
            else if (checkResult == -13)
            {
                messageService.saveAuditMessage(senderId, recverId, param.getType(), param.getData(), "不文明用语");

                hint.setContent("温馨提示：本平台禁止聊天过程中言语攻击、辱骂他人。请文明用语，否则将面临封号风险。");
            }
            else
            {
                hint.setContent("温馨提示：本平台是绿色正规平台。请勿发布违法违规信息，否则将面临封号风险。");

                if (checkResult == -14)
                {
                    // 给接收发提示消息
                    if (sender.getIsWhite() == 0)
                    {
                        ChatPrivateData info = new ChatPrivateData();
                        info.setContent("温馨提示：对方聊天中可能涉及性话题，请考虑是否继续交流。如果您不回复，三条消息后将自动屏蔽对方消息。您也可以将对方加入黑名单");
                        messageService.sendUserMessage(senderId, recverId, Constant.MSG_TYPE_HINT, info, false);
                    }

                    messageService.saveAuditMessage(senderId, recverId, param.getType(), param.getData(), "性话题");
                }
                else
                {
                    messageService.saveAuditMessage(senderId, recverId, param.getType(), param.getData(), "");
                }
            }

            // 给发送者发提示消息
            if (sender.getIsWhite() == 0)
            {
                messageService.sendUserMessage(recverId, senderId, Constant.MSG_TYPE_HINT, hint, false);
            }
        }

/*聊天消息改为收费，因此不再发奖励
        //如果发送方非认证用户，并且接收方非特殊账号并已认证，并且不在通话中，并且不在预约时段内，则给接收方发奖励（连续回复时 isReplay 为 false，所以只会奖励一条）
        if ((!isInBook) && (isReplay == true) && (sender.getAuthRole() == 0) && (recver.getAuthRole() > 0) && (recver.getType() < Constant.ACCOUNT_TYPE_PLAT) && (isInCall == false))
        {
            JSONObject json  = new JSONObject();
            json.set("senderId", senderId);
            json.set("recverId", recverId);

            HttpUtils.send_post_request("http://localhost:8183/scoreAward/chatMessage.php", null, JSONUtil.toJsonStr(json));
        }
*/
        JSONObject json  = new JSONObject();
        json.set("umid", umid);
        if ((checkResult < -11) && (sender.getIsWhite() == 0))
        {
            return ApiResult.failed(10, "内容包含违规信息，发送失败");
        }
        else
        {
            return ApiResult.success(json);
        }
    }

    @ApiOperation("获取消息记录")
    @PostMapping("/getRecord")
    public ApiResult<Object> getRecord(@RequestBody @Valid ChatGetRecordParam param) {
        return ApiResult.success();
    }

    @ApiOperation("用户发送常用语")
    @PostMapping("/sendIdiom")
    public ApiResult<Object> sendIdiom(@RequestBody @Valid ChatSendIdiomParam param) {
        long senderId  = param.getUserId();
        long recverId  = param.getDestId();
        if (senderId == recverId)
        {
            return ApiResult.failed(-10, "接收方信息错误");
        }

        String umid  = TimeUtils.getLocalDateTime("yyyyMMddHHmmssSSS");
        UserBaseInfo sender  = param.getUserInfo();
        //更新用户空闲时间
        userService.setUserIdleTime(sender);

        //发送方是客服
        if (sender.getType() == Constant.ACCOUNT_TYPE_KEFU)
        {
            JSONObject json  = new JSONObject();
            json.set("userId", recverId);
            json.set("kefuId", senderId);
            json.set("type", Constant.MSG_TYPE_TEXT);
            json.set("data", "{\"content\":\"" + param.getContent() + "\"}");

            String ret  = HttpUtils.send_post_request("http://localhost:8183/kefu/sendKefuMessage.php", null, JSONUtil.toJsonStr(json));
            JSONObject res  = JSONUtil.parseObj(ret);
            if (res == null)
            {
                return ApiResult.failed(10, "发送失败，请稍后重试");
            }
                        
            if (res.getInt("code") == 0)
            {
                json  = new JSONObject();
                json.set("umid", umid);                
                return ApiResult.success(json);
            }
            else
            {
                return ApiResult.failed(10, res.getStr("hint"));
            }
        }

        //检查接收方账号状态
        UserBaseInfo recver  = userService.getUserById(recverId);
        if (recver == null)
        {
            return ApiResult.failed(10, "发送失败，对方账号异常");
        }

        if (recver.getStatus() == Constant.ACCOUNT_STATUS_BANNED)
        {
            return ApiResult.failed(10, "对方违规已被封号");
        }

        if (recver.getStatus() == Constant.ACCOUNT_STATUS_DELETED)
        {
            return ApiResult.failed(10, "对方已注销账号");
        }

        //接收方是客服
        if ((recverId == Constant.USERID_SERVER) || (recver.getType() == Constant.ACCOUNT_TYPE_KEFU))
        {
            JSONObject json  = new JSONObject();
            json.set("userId", senderId);
            json.set("type", Constant.MSG_TYPE_TEXT);
            json.set("data", "{\"content\":\"" + param.getContent() + "\"}");

            String ret  = HttpUtils.send_post_request("http://localhost:8183/kefu/sendUserMessage.php", null, JSONUtil.toJsonStr(json));
            JSONObject res  = JSONUtil.parseObj(ret);
            if (res == null)
            {
                return ApiResult.failed(10, "发送失败，请稍后重试");
            }

            if (res.getInt("code") == 0)
            {
                json  = new JSONObject();
                json.set("umid", umid);
                return ApiResult.success(json);
            }
            else
            {
                return ApiResult.failed(10, res.getStr("hint"));
            }            
        }

        //同账号才能互通
        if (!sender.getType().equals(recver.getType()))
        {
            JSONObject json  = new JSONObject();
            json.set("umid", umid);
            return ApiResult.success(json);
        }

        boolean isBlock  = userService.isBlocked(recverId, senderId);
        if (isBlock)
        {
            ChatPrivateData hint  = new ChatPrivateData();
            hint.setContent("发送失败，你已被对方拉黑");
            messageService.sendUserMessage(recverId, senderId, Constant.MSG_TYPE_HINT, hint, false);

            return ApiResult.failed(10, "发送失败，你已被对方拉黑");
        }

        //消息频控：连续发送消息对方未回复，则不能再发送
        int rate  = messageService.addMessageRate(senderId, recverId);
        if (rate > Constant.MSG_TIMES_LIMIT)
        {
            String expire  = "";
            int ttl      = messageService.ttlMessageRate(senderId, recverId);
            int second   = ttl % 60;
            int minutes  = (ttl % 3600) / 60;
            int hours    = ttl / 3600;
            if (hours > 0)
            {
                expire  = hours + "小时" + minutes + "分" + second + "秒";
            }
            else if (minutes > 0)
            {
                expire  = minutes + "分" + second + "秒";
            }
            else
            {
                expire  = second + "秒";
            }

            //给发送方发送提示消息
            ChatPrivateData hint  = new ChatPrivateData();
            hint.setContent("发送失败：对方当前正忙。为避免骚扰，需要对方回复或" + expire + "后您才能继续给对方发送消息。");
            messageService.sendUserMessage(recverId, senderId, Constant.MSG_TYPE_HINT, hint, false);

            JSONObject json  = new JSONObject();
            json.set("umid", umid);
            return ApiResult.success(json);
        }

        ChatPrivateData data  = new ChatPrivateData();
        data.setContent(param.getContent());
        messageService.sendUserMessage(param.getUserId(), param.getDestId(), Constant.MSG_TYPE_TEXT, data, umid, true); 

        JSONObject json  = new JSONObject();
        json.set("umid", umid);
        return ApiResult.success(json);
    }

    @ApiOperation("添加常用语")
    @PostMapping("/addIdiom")
    public ApiResult<Object> addIdiom(@RequestBody @Valid ChatAddIdiomParam param) {
        messageService.addIdiom(param.getUserId(), param.getContent());

        return ApiResult.success("提交成功，请等待审核");
    }    

    @ApiOperation("编辑常用语")
    @PostMapping("/editIdiom")
    public ApiResult<Object> editIdiom(@RequestBody @Valid ChatEditIdiomParam param) {
        messageService.editIdiom(param.getUserId(), param.getId(), param.getContent());

        return ApiResult.success("提交成功，请等待审核");
    }    

    @ApiOperation("删除常用语")
    @PostMapping("/delIdiom")
    public ApiResult<Object> delIdiom(@RequestBody @Valid ChatDelIdiomParam param) {
        messageService.delIdiom(param.getUserId(), param.getId());

        return ApiResult.success();        
    }    

    @ApiOperation("获取常用语")
    @PostMapping("/getIdiom")
    public ApiResult<Object> getIdiom(@RequestBody @Valid ChatGetIdiomParam param) {
        List<UserIdiomInfo> list  = messageService.getIdiomList(param.getUserId(), param.getPage());

         return ApiResult.success(list);
    }
}
